//
//  HNQADetailController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2016/12/30.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNQADetailController.h"
#import "CommentListView.h"
#import "NSString+Size.h"

#define gap 8
#define imageWidth  (kScreen_Width - gap*4)/3

@interface HNQADetailController ()

@property (nonatomic,assign)CGFloat contentHeight;

@property (nonatomic,strong)NSArray *commentDataSourse;

@property (nonatomic,strong)CommentListView *commentListView;

@property (nonatomic,strong)NSDictionary *problemDetailDic;

@end

@implementation HNQADetailController


- (IBAction)sendCoooentAction:(UIButton *)sender {
    if (_commentMsg.text.length < 1) {
        [self showHint:@"请输入评论信息"];
        return;
    }
    [self addComment];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"问题";
}

-(void)drawView{
    _detailHeadImage.layer.masksToBounds = YES;
    _detailHeadImage.layer.cornerRadius = _detailHeadImage.frame.size.width/2;
    [self getProboemListData];
}

-(void)setContentView{
    
    _detailTitle.text = [NSString stringWithFormat:@"%@",[_problemDetailDic objectForKey:@"briefContent"]];
    
    [_detailHeadImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",url_Base,[_problemDetailDic objectForKey:@"photo"]]] placeholderImage:[UIImage imageNamed:@"detailImage.png"]];
    
    _detailHeadName.text = [NSString stringWithFormat:@"%@",[_problemDetailDic objectForKey:@"nickname"]];
     _detailHeadTime.text = [NSString stringWithFormat:@"%@",[_problemDetailDic objectForKey:@"createTime"]];
    
    _detailMsg = [[UILabel alloc]initWithFrame:CGRectMake(gap,_headView.frame.origin.y+_headView.frame.size.height,kScreen_Width - 2*gap,30)];
    _detailMsg.numberOfLines = 1;
    _detailMsg.numberOfLines = 0;
    _detailMsg.clipsToBounds = YES;
    _detailMsg.layer.cornerRadius = 4;
    _detailMsg.textAlignment = NSTextAlignmentLeft;
    _detailMsg.font = [UIFont systemFontOfSize:14];
    //    nameLabel.backgroundColor = RGB(85, 206, 84);
    _detailMsg.textColor = RGBJ(70);
    NSString *detailStr = [NSString stringWithFormat:@"%@",[_problemDetailDic objectForKey:@"content"]];
    if (detailStr.length >0) {
         _detailMsg.text = detailStr;
        CGFloat f = detailStr.length>0 ?[detailStr heightWithFont:[UIFont systemFontOfSize:14.0f] constrainedToWidth:((_detailMsg.frame.size.width) )] : 0;
        CGRect frame = _detailMsg.frame;
        frame.size.height = f;
        _detailMsg.frame = frame;
        
    }else{
        _detailMsg.text = @"暂无详情";
    }
   
    [_contentView addSubview:_detailMsg];
    
    
    [self addImagesView];
    
    _detailCommentCount = [[UILabel alloc]initWithFrame:CGRectMake(0,_detailImageScrollView.frame.origin.y+_detailImageScrollView.frame.size.height,kScreen_Width,40)];
    _detailCommentCount.numberOfLines = 1;
    _detailCommentCount.clipsToBounds = YES;
    _detailCommentCount.layer.cornerRadius = 4;
    _detailCommentCount.textAlignment = NSTextAlignmentLeft;
    _detailCommentCount.font = [UIFont systemFontOfSize:15];
    _detailCommentCount.backgroundColor = RGBJ(244);
    _detailCommentCount.textColor = RGBJ(70);
    _detailCommentCount.text = @"  5条回答个数";
    [_contentView addSubview:_detailCommentCount];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _detailCommentCount.frame.origin.y, kScreen_Width, 0.5)];
    lineLabel.backgroundColor = RGBJ(225);
    [_contentView addSubview:lineLabel];
    
    _contentHeight = _detailCommentCount.frame.origin.y+_detailCommentCount.frame.size.height;
    _contentView.contentSize = CGSizeMake(kScreen_Width, _detailCommentCount.frame.origin.y+_detailCommentCount.frame.size.height);
    
    [self getCommentListData];

}

-(void)addImagesView{
    _detailImageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _detailMsg.frame.origin.y+_detailMsg.frame.size.height, kScreen_Width, imageWidth + 2*gap)];
    _detailImageScrollView.bounces = NO;
    NSArray *imageArr = [[NSString stringWithFormat:@"%@",[_problemDetailDic objectForKey:@"image"]] componentsSeparatedByString:@","];
    for (int i = 0; i < imageArr.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(gap + (gap + imageWidth)*i, gap, imageWidth, imageWidth)];
         [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",url_Base,imageArr[i]]] placeholderImage:[UIImage imageNamed:@"detailImage.png"]];
        [_detailImageScrollView addSubview:imageView];
    }
    _detailImageScrollView.contentSize = CGSizeMake(gap + (gap + imageWidth)*imageArr.count , imageWidth + 2*gap);
    
    
    [_contentView addSubview:_detailImageScrollView];
}

-(void)addCommentListView{
    CGSize size = _contentView.contentSize;
    NSLog(@"---------------------%f",size.height);
    
    NSLog(@"---------------------self.dataSourse= %@",self.commentDataSourse);
    
      _detailCommentCount.text = [NSString stringWithFormat:@"  %ld条回答个数",_commentDataSourse.count];
    _commentListView = [[CommentListView alloc]initWithCGRect:CGRectMake(0,  _contentHeight, _contentView.contentSize.width, _contentView.contentSize.height) withData:self.commentDataSourse];
    
    [self.contentView addSubview:_commentListView];
    
    NSLog(@"%f",_commentListView.frame.size.height);
    
    size.height = size.height + _commentListView.frame.size.height;
    _contentView.contentSize = size;
    
//    _contentView.frame = CGRectMake(0, 0, kScreen_Width, kScreen_Height*5);
}


#pragma mark - 初始化问题详情
-(void)getProboemListData{
    
    
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",_postId,@"postId",nil];
    
    NSLog(@"param = %@",param);
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_problem_list parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取医生列表失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary *obj = [data objectForKey:@"obj"];
            _problemDetailDic = [obj objectForKey:@"result"][0];
            NSLog(@"------obj = %@",_problemDetailDic);
            
            [self setContentView];
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
      
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取医生列表失败"];
    
    }];
    
    
}

#pragma mark - 初始化评论列表数据
-(void)getCommentListData{
    
    
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",_postId,@"postId",nil];
    
    NSLog(@"param = %@",param);
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_problem_comment_list parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取问题评论列表失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSArray *obj = [data objectForKey:@"obj"];
            
            _commentDataSourse = obj;
            
            
            NSLog(@"gfdkgdfhhj----%@",_commentDataSourse);
            
            
            [self addCommentListView];
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取问题评论列表失败"];
        
    }];
    
    
}

#pragma mark - 添加评论
-(void)addComment{
    
    
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",_postId,@"postId",_commentMsg.text,@"content",nil];
    
    NSLog(@"param = %@",param);
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_problem_comment_create parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"评论医生失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
          
            _commentMsg.text = @"";
        
            [_commentMsg resignFirstResponder];
//            [self addCommentListView];
        
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"评论医生失败"];
        
    }];
    
    
}







@end
